#!/bin/bash
set -e

name="alpine-chrome"
registry="gcr.io/zenika-hub"
CURRENT_VERSION_CHROMIUM=$(docker container run --rm --entrypoint "" zenika/$name chromium-browser --version)
tag=${CURRENT_VERSION_CHROMIUM:9:2}
echo "🚀 $name 👉 $tag"

tagStart=$(expr index "$IMAGE_NAME" :)
repoName=${IMAGE_NAME:0:tagStart-1}
docker tag "$IMAGE_NAME" "${repoName}:${tag}"
docker push "${repoName}:${tag}"

docker version
echo "$GCLOUD_SERVICE_KEY" | base64 -d > 'key.json'
cat key.json | docker login -u _json_key --password-stdin "https://$registry"

for published_tag in "latest" $tag; do
    docker tag "$IMAGE_NAME" "$registry/$name:$published_tag"
    docker push "$registry/$name:$published_tag"
    for region in "us" "eu" "asia"; do
        #publish in the different regions
        docker login -u _json_key -p "$(cat key.json)" "https://$region.$registry"
        docker tag "$registry/$name" "$region.$registry/$name:$published_tag"
        docker push "$region.$registry/$name:$published_tag"
    done
done
